<?php 
use addons\hellozjx_mapwxapp\inc\page\Map;
use addons\hellozjx_mapwxapp\inc\page\Setting;
use addons\hellozjx_mapwxapp\inc\page\Teacher;
use addons\hellozjx_mapwxapp\inc\page\User;

/**
 * 在线教育小程序模块订阅器
 *
 * @author vcdes
 * @url 
 */
defined('IN_IA') or exit('Access Denied');
// 实现自动加载
spl_autoload_register(function ($class_name) {
    $class = dirname(dirname(dirname(__FILE__))) . '/' . str_replace('\\', '/', $class_name) .'.php';

    if (file_exists($class)) {
        require_once $class;
    } else {
        throw new Exception("文件无法加载，检测文件是否存在路径：$class");
    }
});
 
require_once 'inc/common/function.php';

class hellozjx_mapwxappModuleWxapp extends WeModuleWxapp
{
    use Map, Setting, Teacher, User;

    public $module_name = 'hellozjx_mapwxapp';
	public $table_area = 'hellozjx_mapwxapp_area';
	public $table_shop = 'hellozjx_mapwxapp_shop';

	public function doPageDetail()
    {
        global $_W, $_GPC;
        $uniacid = $_W['uniacid'];
        $id = intval($_GPC['id']);
        $shop = pdo_getall($this->table_shop, array('uniacid' => $uniacid, 'id' => $id, 'enabled' => 1));
        if ($shop) 
		{
			
            $shop['thumb'] = tomedia($shop['thumb']);
            $shop['createtime'] = date('Y-m-d H:i:s', $shop['createtime']);
            $shop['shop_lat'] = (double) $shop['shop_lat_t'];
            $shop['shop_lng'] = (double) $shop['shop_lng_t'];
            message(error(0, $shop), '', 'ajax');
        } else {
            message(error(1, '私塾不存在或已隐藏'), '', 'ajax');
        }
    }
	public function doPageMapIndex()
    {
        global $_W, $_GPC;
        $uniacid = $_W['uniacid'];
        $area_id = intval($_GPC['area_id']);
        $where = array('uniacid' => $uniacid, 'enabled' => 1);
        if ($area_id > 0) {
            $where['area_id'] = array(0, $area_id);
        }
		$config = $this->module['config'];
        $areas = pdo_getall($this->table_room, array('uniacid' => $uniacid), array('id', 'name'), '', array('sort asc'));
        $area = array(array('id'=>0,'name'=>'全部'));
        if (!empty($areas)) {
            foreach ($areas as $key => $value) {
                $area[] = array('id'=>$value['id'],'name'=>$value['name']);
            }
        }
		$config['icon']=tomedia($config['icon']);
        $data = array('area' => $area ?: array(),'config'=>$config);
        message(error(0, $data), '', 'ajax');
    }
    // public function doPageGetMapList()
    // {
    //     $obj = new Map();
    //     return $obj->getMapList();
    // }
	/*public function doPageGetMapList()
    {
        global $_W, $_GPC;
        $uniacid = $_W['uniacid'];
        $area_id = intval($_GPC['area_id']);
		$lat=$_GPC['lat'];
		$lng=$_GPC['lng'];
		$config = $this->module['config'];
        $where = array('uniacid' => $uniacid, 'enabled' => 1);
        if ($area_id > 0) {
            $where['area_id'] = array(0, $area_id);
        }
        $shop = pdo_getall($this->table_shop, $where, array('id','name','logo','shop_lat','shop_lng','shop_lat_t','shop_lng_t'), '', array('displayorder desc', 'id desc'));
		$arr=array();
        if (!empty($shop)) {
            foreach ($shop as $key => $value) {
				$d= $this->getDistance($value['shop_lat_t'], $value['shop_lng_t'], $lat, $lng);
				if($d>$config['distance']&&$config['distance']>0)
				{
					continue;
				}
				else
				{
					$arr[$key]=$value;
					$arr[$key]['dist']=$d;
					$arr[$key]['name'].=",距您".$d."千米";
					$arr[$key]['logo'] = tomedia($value['logo']);
				}
            }
        }
		$arr = array_values($arr);
        $total = pdo_getcolumn($this->table_shop, $where, 'count(*)');
        $data = array('list' => $arr ?: array(), 'total' => $total ? intval($total) : 0);
        message(error(0, $data), '', 'ajax');
    }*/
	function getDistance($lat1, $lng1, $lat2, $lng2, $len_type = 1, $decimal = 2)
    {
        $radLat1 = $lat1 * M_PI / 180;
        $radLat2 = $lat2 * M_PI / 180;
        $a = $lat1 * M_PI / 180 - $lat2 * M_PI / 180;
        $b = $lng1 * M_PI / 180 - $lng2 * M_PI / 180;
        $s = 2 * asin(sqrt(pow(sin($a / 2), 2) + cos($radLat1) * cos($radLat2) * pow(sin($b / 2), 2)));
        $s = $s * 6371;
        $s = round($s * 1000);
        if ($len_type > 1) {
            $s /= 1000;
        }
        $s /= 1000;
        return round($s, $decimal);
    }

    /**
     * 支付回调方法
     * @param  [type] $log [description]
     * @return [type]      [description]
     */
     public function payResult($log)
     {
        $input = file_get_contents('php://input');
        $obj = isimplexml_load_string($input, 'SimpleXMLElement', LIBXML_NOCDATA);
        $data = json_decode(json_encode($obj), true);
        file_put_contents('./a.log', '['. date('Y-m-d H:i:s') . ']  ' .json_encode($data) ."\n" . "=============", FILE_APPEND);
        // 查出当前视频
        $order = pdo_get($this->table_order, array('payment_no' => $data['out_trade_no']));
        // 查出老师 id
        $video = pdo_get($this->table_video, array('id' => $order['vid']));

        if ($data['return_code'] == 'SUCCESS') {
            // 修改订单支付状态
            pdo_update($this->table_order, array('pay_time' => date('Y-m-d H:i:s'), 'status' => 2), array('payment_no' => $data['out_trade_no']));
            // 老师增加收入
            pdo_update($this->table_teacher, array('money +=' => $video['price']), array('id' => $video['teacher_id']));
            //明细还未添加
            
            exit('SUCCESS');
        }
        exit('error');
     }
}